#include "stm32f10x.h"                  // Device header

void Ic_Init(void)
{
	TIM_ICInitTypeDef  TIM_IcInitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstructure;
	//TIM_OCInitTypeDef TIM_OCInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure; //这个要前放,不然会有警告
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //开启时钟
	
	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//	 GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);  //冲映射PinA15
//	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

	GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	TIM_InternalClockConfig(TIM3);
	TIM_TimeBaseInitstructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitstructure.TIM_CounterMode =TIM_CounterMode_Up ;
	TIM_TimeBaseInitstructure.TIM_Period = 65536-1;    //ARR  
	TIM_TimeBaseInitstructure.TIM_Prescaler =72-1;   //PSC   预分频
	TIM_TimeBaseInitstructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitstructure);
	
	
	TIM_IcInitStructure.TIM_Channel =TIM_Channel_1 ;
	TIM_IcInitStructure.TIM_ICFilter =0xF ;
	TIM_IcInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising ;     //输入捕获
	TIM_IcInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1 ;
	TIM_IcInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI ;
	TIM_ICInit(TIM3,&TIM_IcInitStructure);
	
	TIM_PWMIConfig(TIM3,&TIM_IcInitStructure);
	
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	TIM_Cmd(TIM3,ENABLE);
	
}

uint32_t IC_GetFreq(void)
{
	  return 1000000/(TIM_GetCapture1(TIM3));
}

uint32_t IC_GetDuty(void)
{
	return (TIM_GetCapture2(TIM3)+1)*100/TIM_GetCapture1(TIM3);
}




